package com.tsing.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.entity.TlLetter;
import com.tsing.pojo.vo.LetterVO;
import com.tsing.service.LetterService;
import com.tsing.service.impl.LetterServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/15 20:48
 */
public class LetterController {
    private final LetterService letterService = new LetterServiceImpl();

    public PageResult<LetterVO> list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : 1;

        Integer senderId = jsonObject.getInteger("senderId");
        senderId = (senderId != null && senderId > 0) ? senderId : 1;

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;


        PageResult<LetterVO> result = letterService.list(page, size, userId, senderId);


        return result;
    }

    public Result send(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = jsonObject.getInteger("userId");
        Integer senderId = jsonObject.getInteger("senderId");
        String content = jsonObject.getString("content");

        Integer result = letterService.send(userId, senderId, content);
        if (result > 0) {
            return Result.success(result);
        }

        return Result.error("发送失败");
    }

    public PageResult<LetterVO> latestMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : 1;

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;


        PageResult<LetterVO> result = letterService.latestMessage(page, size, userId);


        return result;
    }

}
